package com.similarworld.bean;

import java.io.IOException;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.similarworld.model.User;
import com.similarworld.util.SecurityUtils;

/**
 * Provide methods to login. 
 * 
 * @author ebitik
 * */
@Component("loginBean")
@Scope("session")
public class LoginBean {
	
	private User loginUser;
	
	public String login() throws IOException, ServletException {
		ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();

		RequestDispatcher dispatcher = ((ServletRequest) context.getRequest())
		.getRequestDispatcher("/j_spring_security_check");

		dispatcher.forward((ServletRequest) context.getRequest(),
				(ServletResponse) context.getResponse());

		FacesContext.getCurrentInstance().responseComplete();
		
		return null;
	}

	public User getLoginUser() {
		return SecurityUtils.getLoginUser();
	}

}